const { useEnv, args, rawArgv } = require('./init.js')

// 处理代理
handleProxyRule()
start()


function start(){
  const command = args._[0]
  // rawArgv.push('--mode', useEnv.NODE_ENV)
  // rawArgv.push('--mode', 'false')
  console.log(`运行：vue-cli-service ${rawArgv.join(' ')}`);

  // 只构建新版浏览器
  // useEnv.VUE_CLI_MODERN_BUILD = true;

  const Service = require('./ServiceWrap.js')
  const service = new Service(process.env.VUE_CLI_CONTEXT || process.cwd())

  // args.mode = 'false'
  args.mode = useEnv.NODE_ENV
  args._ = [command]

  Object.assign(process.env, useEnv)
  global.useEnv = useEnv;
  service.run(command, args, rawArgv).then(() => {
    if (useEnv.NODE_ENV == 'production'){
      console.log("打包完毕");
      if (!process.env.AGAIN_BUILD){
        buildImage();
      }
    }
  }).catch(err => {
    console.error(err);
    process.exit(1)
  })
  // buildImage();

}



function handleProxyRule() {
  if (useEnv.NODE_ENV != 'development') return

  let rules = useEnv.VUE_APP_PROXY_RULE
  let proxyRule
  try {
    proxyRule = JSON.parse(rules)
  } catch (e) {
    try {
      proxyRule = eval(rules)
    } catch (e) {
      proxyRule = eval('(' + rules + ')')
    }
  }
  useEnv.VUE_APP_PROXY_RULE = proxyRule || {}
}


function buildImage(){
  if (args.docker){
    const {build} = require('../DockerBuilder/docker.js')
    build()
  }
}
